<template>
	<view class="container">
		<view class="profile-card">
			<view class="avatar-section">
				<image class="avatar" :src="userInfo.avatar || '/static/default-avatar.png'"></image>
				<view class="user-info">
					<text class="username">{{userInfo.username || '未登录'}}</text>
					<text class="user-id" v-if="userInfo.id">ID: {{userInfo.id}}</text>
				</view>
			</view>
			
			<view class="stats-section">
				<view class="stat-item">
					<text class="stat-number">{{myWishes.length}}</text>
					<text class="stat-label">我的心愿</text>
				</view>
			</view>
		</view>
		
		<view class="my-wishes-card">
			<view class="card-header">
				<text class="card-title">我的心愿</text>
			</view>
			
			<scroll-view scroll-y class="wish-list" @refresherrefresh="onRefresh" refresher-enabled>
				<view class="no-data" v-if="myWishes.length === 0">
					<text>暂无心愿记录</text>
				</view>
				
				<view v-else>
					<view 
						class="wish-item" 
						v-for="(item, index) in myWishes" 
						:key="index"
					>
						<view class="wish-content">
							<text>{{item.content}}</text>
						</view>
						<view class="wish-footer">
							<text class="time">{{formatTime(item.created_at)}}</text>
							<view class="footer-actions">
								<view class="status-tag" :class="{'status-public': item.is_public}">
									<text>{{item.is_public ? '公开' : '私密'}}</text>
								</view>
								<view class="delete-btn" @click.stop="confirmDelete(item.id)">
									<text>删除</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		
		<view class="logout-card">
			<button class="logout-btn" @click="handleLogout">退出登录</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {},
				myWishes: [],
				isLoading: false
			}
		},
		onLoad() {
			// 检查登录状态
			const token = uni.getStorageSync('token');
			if (!token) {
				uni.reLaunch({
					url: '/pages/login/login'
				});
				return;
			}
			
			this.loadUserInfo();
			this.loadMyWishes();
		},
		onShow() {
			// 页面每次显示时刷新数据
			const token = uni.getStorageSync('token');
			if (!token) {
				uni.reLaunch({
					url: '/pages/login/login'
				});
				return;
			}
			this.loadMyWishes();
		},
		methods: {
			loadUserInfo() {
				this.$api.getUserInfo().then(res => {
					this.userInfo = res;
				}).catch(err => {
					uni.showToast({
						title: '获取用户信息失败',
						icon: 'none'
					});
				});
			},
			
			loadMyWishes() {
				if (this.isLoading) return;
				
				this.isLoading = true;
				this.$api.getMyWishes().then(res => {
					this.myWishes = res;
				}).catch(err => {
					uni.showToast({
						title: '获取心愿列表失败',
						icon: 'none'
					});
				}).finally(() => {
					this.isLoading = false;
				});
			},
			
			onRefresh() {
				this.loadMyWishes();
				setTimeout(() => {
					uni.stopPullDownRefresh();
				}, 1000);
			},
			
			viewWishDetail(id) {
				// 暂时不需要跳转到详情页
				// uni.navigateTo({
				//	url: `/pages/wish-detail/wish-detail?id=${id}`
				// });
			},
			
			handleLogout() {
				uni.showModal({
					title: '提示',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							// 清除登录信息
							uni.removeStorageSync('token');
							uni.removeStorageSync('userInfo');
							
							// 跳转到登录页
							uni.reLaunch({
								url: '/pages/login/login'
							});
						}
					}
				});
			},
			
			formatTime(timestamp) {
				const date = new Date(timestamp);
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				const hour = String(date.getHours()).padStart(2, '0');
				const minute = String(date.getMinutes()).padStart(2, '0');
				return `${year}-${month}-${day} ${hour}:${minute}`;
			},
			
			confirmDelete(id) {
				uni.showModal({
					title: '提示',
					content: '确定要删除这个心愿吗？',
					success: (res) => {
						if (res.confirm) {
							this.deleteWish(id);
						}
					}
				});
			},
			
			deleteWish(id) {
				uni.showLoading({
					title: '删除中...'
				});
				
				this.$api.deleteWish(id).then(res => {
					uni.hideLoading();
					
					uni.showToast({
						title: '删除成功',
						icon: 'success'
					});
					
					// 重新加载心愿列表
					this.loadMyWishes();
				}).catch(err => {
					uni.hideLoading();
					console.error('删除心愿失败:', err);
					uni.showToast({
						title: '删除失败，请重试',
						icon: 'none',
						duration: 2000
					});
				});
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		background-color: #f5f7fa;
		padding: 30rpx;
		box-sizing: border-box;
	}
	
	/* 个人资料卡片 */
	.profile-card {
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 40rpx 30rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
		position: relative;
		overflow: hidden;
	}
	
	.avatar-section {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
	}
	
	.avatar {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		background-color: #f0f2f5;
		box-shadow: 0 4rpx 15rpx rgba(0, 0, 0, 0.1);
		border: 2rpx solid #ffffff;
	}
	
	.user-info {
		margin-left: 30rpx;
		display: flex;
		flex-direction: column;
	}
	
	.username {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 10rpx;
	}
	
	.user-id {
		font-size: 24rpx;
		color: #999;
	}
	
	.stats-section {
		display: flex;
		border-top: 1rpx solid #f0f2f5;
		padding-top: 30rpx;
	}
	
	.stat-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.stat-number {
		font-size: 36rpx;
		font-weight: bold;
		color: #6159e6;
		margin-bottom: 6rpx;
	}
	
	.stat-label {
		font-size: 24rpx;
		color: #999;
	}
	
	/* 心愿列表卡片 */
	.my-wishes-card {
		background-color: #ffffff;
		border-radius: 20rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
		overflow: hidden;
		flex: 1;
		display: flex;
		flex-direction: column;
	}
	
	.card-header {
		padding: 30rpx;
		border-bottom: 1rpx solid #f0f2f5;
	}
	
	.card-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}
	
	.wish-list {
		flex: 1;
		padding: 20rpx;
		max-height: 800rpx;
	}
	
	.no-data {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 200rpx;
		color: #999;
		font-size: 28rpx;
	}
	
	.wish-item {
		background-color: #f9fafc;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
		transition: all 0.3s ease;
	}
	
	.wish-item:active {
		transform: scale(0.98);
	}
	
	.wish-content {
		font-size: 32rpx;
		color: #333;
		line-height: 1.5;
		margin-bottom: 20rpx;
	}
	
	.wish-footer {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 24rpx;
		color: #999;
	}
	
	.footer-actions {
		display: flex;
		align-items: center;
	}
	
	.status-tag {
		padding: 6rpx 20rpx;
		border-radius: 30rpx;
		background-color: #f0f2f5;
		margin-right: 15rpx;
		font-size: 22rpx;
	}
	
	.status-public {
		background-color: #e6eeff;
		color: #6159e6;
	}
	
	.delete-btn {
		padding: 6rpx 20rpx;
		border-radius: 30rpx;
		background-color: #fff1f0;
		color: #ff4d4f;
		font-size: 22rpx;
	}
	
	.delete-btn:active {
		opacity: 0.8;
	}
	
	/* 退出登录卡片 */
	.logout-card {
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
	}
	
	.logout-btn {
		width: 100%;
		height: 90rpx;
		border-radius: 45rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		background: linear-gradient(135deg, #ff6a6a, #ff3a30);
		color: #fff;
		box-shadow: 0 4rpx 15rpx rgba(255, 58, 48, 0.3);
		border: none;
		transition: all 0.3s ease;
	}
	
	.logout-btn:active {
		transform: scale(0.98);
	}
</style> 